package com.mini.mysql.service;

import com.mini.mysql.model.AttnClassInfo;
import com.mini.mysql.model.AttnStudentOrder;
import com.mini.mysql.model.Dept;
import com.mini.mysql.repository.AttnClassInfoRepository;
import com.mini.mysql.repository.AttnStudentOrderRepository;
import com.mini.mysql.repository.DeptRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ClassService {

    @Autowired
    private AttnClassInfoRepository attnClassInfoRepository;

    @Autowired
    private AttnStudentOrderRepository attnStudentOrderRepository;

    @Autowired
    private DeptRepository deptRepository;


    public List<AttnClassInfo> getClassSimpleInfo(Collection<Long> classIds){
        if(CollectionUtils.isEmpty(classIds)){
            return null;
        }

        List<AttnClassInfo> classSimples = attnClassInfoRepository.findAllById(classIds);
        return classSimples;

    }


    public Map<Long, AttnClassInfo> mapClassSimpleInfo(Collection<Long> classIds){
        Map<Long, AttnClassInfo> map = new HashMap<>();
        List<AttnClassInfo> list = this.getClassSimpleInfo(classIds);

        if(CollectionUtils.isEmpty(list)){
            return map;
        }

        map = list.stream().collect(Collectors.toMap(AttnClassInfo::getId, o->o, (o1,o2)->o2));
        return map;
    }

    public List<AttnStudentOrder> getClassOrder(Collection<String> orderNos){
        if(CollectionUtils.isEmpty(orderNos)){
            return null;
        }

        return this.attnStudentOrderRepository.findAllByOrderNoIn(orderNos);
    }

    public List<Dept>  findByPid(Long pid){
        return deptRepository.findAllByPid(pid);
    }


}
